// pages/zixun/zixunnew/zixunnew.js
Page({
  getCurrentTime() {
    const date = new Date();
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  },

  /**
   * 页面的初始数据
   */
  data: {
    detail:{},
    value:'',
    formData:{}
  },
  /**
   * 输入框实时回调
   * @param {*} options 
   */
  inputText: function (e) {
    //获取输入框输入的内容
    const field = e.currentTarget.dataset.field;
    const value = e.detail.value;
    this.setData({
      ['formData.' + field]: value
    });
  },
  submitData:function () {
    const user = wx.getStorageSync('UserInfo');
    const detail = this.data.detail;
    const content = this.data.formData.content;

    if (!content) {
      wx.showToast({
        title: '请输入内容',
        icon: 'none'
      });
      return;
    }

    const currentTime = this.getCurrentTime();
    const dataToSend = {
      id: detail.id,
      createTime: currentTime,
      content: content,
      consultId: detail.id,
      userId: user.id
    };
    wx.request({
      url: 'http://localhost:8080/appointment/insertQuestion',
      method: 'POST',
      data: dataToSend,
      header: {
        'Content-Type': 'application/json'
      },success:function (res) {
        if(res.statusCode===200){
          wx.showToast({
            title: '预约成功',
          })
        }else{
            wx.showToast({
              title: '预约失败',
            })
        }
      },
      fail:function (err) {
        wx.showToast({
          title: '请求失败',
        });
        console.error(err);
      }
    })
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    const eventChannel = this.getOpenerEventChannel();
    const detail = await new Promise((resolve) => {
      eventChannel.on('acceptDataFromOpenerPage', (data) => {
        console.log(data.data);
        resolve(data.data);
      });
    });
    this.setData({
      detail: detail
    });
  },
})
